﻿using CalligraphyWeb;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace Calligraphy.Models
{
    public class FontFamily
    {
        #region Data Members
        private fontFamily data;
        public fontFamily Data
        {
            get
            {
                if (data == null)
                    data = new fontFamily();

                return data;
            }
            set
            {
                if (data == value) return;
                data = value;
            }
        }
        #endregion

        #region Class Methods
        public FontFamily()
        {
        }

        public FontFamily(int id)
        {
            Get(id);
        }

        public FontFamily(fontFamily obj)
        {
            Get(obj);
        }
        public FontFamily Get(int id)
        {
            using (calligraphyEntities db = new calligraphyEntities())
            {
                var query = from c in db.fontFamilies
                            where c.id == id
                            select c;

                if (query.Count() > 0)
                {
                    var obj = query.FirstOrDefault();
                    return Get(obj);
                }
                else
                    return null;
            }
        }

        public FontFamily Get(fontFamily obj)
        {
            if (this.data == obj) return this;

            this.data = obj;
            return this;
        }

        private long Insert()
        {
            using (calligraphyEntities db = new calligraphyEntities())
            {
                db.fontFamilies.Add(data);
                db.SaveChanges();
                return data.id;
            }
        }

        private long Update()
        {
            using (calligraphyEntities db = new calligraphyEntities())
            {
                db.fontFamilies.Attach(data);
                db.Entry(data).State = System.Data.EntityState.Modified;
                db.SaveChanges();
                return data.id;
            }
        }

        public long Save()
        {
            if (data.id == 0)
                return Insert();
            else
                return Update();
        }

        public long Delete()
        {
            using (calligraphyEntities db = new calligraphyEntities())
            {
                db.fontFamilies.Remove(data);
                db.SaveChanges();
                return data.id;
            }
        }
        #endregion

        #region Static Methods
        public static ObservableCollection<fontFamily> Get(string sortColumn = "id", bool isAsc = false,
                                                    int pageIndex = 1, int pageSize = int.MaxValue)
        {
            using (calligraphyEntities db = new calligraphyEntities())
            {
                IQueryable<fontFamily> query = db.fontFamilies.AsQueryable();


                query = Sorting(query, sortColumn, isAsc);
                query = Paging(query, pageIndex, pageSize);

                if (query == null)
                    return new ObservableCollection<fontFamily>();
                else
                    return new ObservableCollection<fontFamily>(query.ToList());
            }
        }

        public static List<long> Save(Collection<fontFamily> collection)
        {
            var result = new List<long>();
            foreach (var obj in collection)
            {
                var temp = new FontFamily(obj);
                result.Add(temp.Save());
            }

            return result;
        }

        public static int Delete(Collection<fontFamily> collection)
        {
            int result = 0;
            using (calligraphyEntities db = new calligraphyEntities())
            {
                foreach (var obj in collection)
                {
                    db.fontFamilies.Remove(obj);
                    result++;
                }

                db.SaveChanges();
            }
            return result;
        }

        private static IQueryable<fontFamily> Sorting(IQueryable<fontFamily> query, string sortColumn = "id", bool isAsc = false)
        {
            switch (sortColumn)
            {
                case "name":
                    query = isAsc ? query.OrderBy(o => o.name) : query.OrderByDescending(o => o.name);
                    break;
                case "filename":
                    query = isAsc ? query.OrderBy(o => o.filename) : query.OrderByDescending(o => o.filename);
                    break;
                case "value":
                    query = isAsc ? query.OrderBy(o => o.value) : query.OrderByDescending(o => o.value);
                    break;
               
                case "id":
                default:
                    query = isAsc ? query.OrderBy(o => o.id) : query.OrderByDescending(o => o.id);
                    break;
            }

            return query;
        }

        private static IQueryable<fontFamily> Paging(IQueryable<fontFamily> query, int pageIndex = 1, int pageSize = 10)
        {
            if (pageIndex <= 1)
                pageIndex = 0;
            else
                pageIndex--;

            return query.Skip(pageIndex * pageSize).Take(pageSize);
        }

        public static ObservableCollection<fontFamily> Search(string name = "", string value = "",
                                        string filename = "",
                                        string sortColumn = "id", bool isAsc = false,
                                        int pageIndex = 1, int pageSize = 10)
        {
            using (calligraphyEntities db = new calligraphyEntities())
            {
                var query = from c in db.fontFamilies
                            where (string.IsNullOrEmpty(name) ? true : c.name.ToLower().Contains(name.ToLower()))
                            && (string.IsNullOrEmpty(value) ? true : c.value.ToLower().Contains(value.ToLower()))
                            && (string.IsNullOrEmpty(filename) ? true : c.filename.ToLower().Contains(filename.ToLower()))
                            
                            select c;

                query = Sorting(query, sortColumn, isAsc);
                query = Paging(query, pageIndex, pageSize);

                if (query.Count() > 0)
                    return new ObservableCollection<fontFamily>(query.ToList());
                else
                    return new ObservableCollection<fontFamily>();
            }
        }

        public static List<SelectListItem> GetSelectListItems(string selected = "")
        {
            List<SelectListItem> results = new List<SelectListItem>();
            var fontsList = FontFamily.Get();

            foreach (var iter in fontsList)
            {
                results.Add(new SelectListItem()
                {
                    Text = iter.name,
                    Value = iter.name,
                    Selected = selected.ToLower().Equals(iter.value)
                });
            }
            return results;
        }
        #endregion
    }
}
